package com.tencent.sr.iris.activity.service.event.task;

import com.alibaba.fastjson.JSON;
import com.tencent.sr.iris.activity.common.enums.TaskRecordAwardIssueStatusEnum;
import com.tencent.sr.iris.activity.domain.event.dto.ActivityDTO;
import com.tencent.sr.iris.activity.domain.event.dto.ActivityTaskDTO;
import com.tencent.sr.iris.activity.interfaces.event.dto.ActivityEventDTO;
import com.tencent.sr.iris.activity.repository.entity.TIrisActivityAwardRecordDO;
import com.tencent.sr.iris.activity.repository.entity.TIrisUserTaskRecordDO;
import com.tencent.sr.iris.activity.service.activity.ActivityCommonService;
import com.tencent.sr.iris.activity.service.activity.IrisUserTaskRecordService;
import com.tencent.sr.iris.activity.service.event.vo.BaseUserTaskDataVO;
import com.tencent.sr.iris.activity.service.event.vo.TaskExecutorExtendInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.List;
import java.util.Objects;

/**
 * @author xiaojun
 */
@Slf4j
@Component
public class UserLevelUpPromoTaskExecutor extends AbstractIrisTaskExecutor {

    @Autowired
    private IrisUserTaskRecordService userTaskRecordService;

    @Autowired
    private ActivityCommonService activityCommonService;

    @Override
    public void process(ActivityEventDTO msg, ActivityDTO activity, ActivityTaskDTO task,
            List<String> eventHandingInfoList, TaskExecutorExtendInfo taskExecutorExtendInfo) {
        try {
            // 查询任务完成进度
            TIrisUserTaskRecordDO userTaskRecordDO = queryUserTaskRecord(msg.getUid(), activity.getBizId(),
                    task.getBizId(), null);
            log.info("升级有礼(晋升奖)事件处理，当前任务完成进度:{}", JSON.toJSONString(userTaskRecordDO));
            if (Objects.nonNull(userTaskRecordDO)) {
                if (Objects.equals(TaskRecordAwardIssueStatusEnum.ISSUE_SUCCEED.getCode(),
                        userTaskRecordDO.getAwardIssueStatus())) {
                    log.info("升级有礼(晋升奖)，下级用户已完成任务,uid:{},ActivityEventDTO:{},", msg.getUid(), JSON.toJSONString(msg));
                    return;
                }
            } else {
                //新增用户活动任务完成进度
                userTaskRecordDO = buildUserTaskRecordDO(activity, task, msg);
                log.info("升级有礼(晋升奖)任务达成,uid:{},ActivityEventDTO:{}", msg.getUid(), JSON.toJSONString(msg));
            }
            //发放权益
            TIrisActivityAwardRecordDO awardRecordDO = activityCommonService.sendAward(userTaskRecordDO, task);
            //记录任务进度
            userTaskRecordService.updateUserTaskRecordStatus(userTaskRecordDO, awardRecordDO);
        } catch (Exception e) {
            log.error(this.getClass().getSimpleName() + "执行异常", e);
            log.error(this.getClass().getSimpleName() + "执行异常,event:{},activity:{},task:{}",
                    JSON.toJSONString(msg), JSON.toJSONString(activity), JSON.toJSONString(task), e);
        }

    }

    @Override
    public void fillTaskRecordStatus(ActivityEventDTO eventDTO, ActivityDTO act, ActivityTaskDTO task,
            TIrisUserTaskRecordDO userTaskRecordDO, LocalDate currentLocalDate) {

    }

    @Override
    public boolean executeAward(ActivityEventDTO event, ActivityDTO act, ActivityTaskDTO task) {
        return false;
    }

    @Override
    public boolean checkTaskThreshold(ActivityEventDTO event, ActivityDTO activity, ActivityTaskDTO task,
            BaseUserTaskDataVO userTaskDataVO) {
        return false;
    }

    @Override
    public BaseUserTaskDataVO prepareUserTaskData(ActivityEventDTO event, ActivityDTO act, ActivityTaskDTO task) {
        return null;
    }
}
